//
//  TemperatureModel.swift
//  MYModule
//
//  Created by 尹琼 on 2022/12/19.
//

import UIKit
import SQLite
import SwiftDate
import HandyJSON
import SQLiteValueExtension
import MYModule

open class TemperatureModel: NSObject, FullDaySQLite, MYSQLiteTableiabe, HandyJSON {
    
    public var adress: String = ""
    
    public var id: Int64 = 0
    
    public var date: Date = Date().sqlDate()
    
    /// 每5分钟测量一次
    public var tempeartures: [Double] = [] {
        
        didSet {
            
            /// 过滤 
            tempeartures = tempeartures.map({
                
                if $0 >= 30 && $0 <= 42 {
                    return $0
                }
                
                return 0
            })
            
        }
        
    }
    
    var updateTime: Double = 0
    
    public var isEmpty: Bool {
        
        return tempeartures.filter({ $0 > 0 }).isEmpty
        
    }
    
    public var avg: Double {
        
        let tempeartures = tempeartures.filter({ $0 > 0 })
        if tempeartures.isEmpty {
            return 0
        }
        
        return tempeartures.reduce(0, { $0 + $1 })/Double(tempeartures.count)
    }
    
    required public override init() {}
    
    
    public func updateData() {
        
        // 体温只保存 30 - 42 度
        tempeartures = tempeartures.map({
            
            if $0 >= 30 && $0 <= 42 {
                return $0
            }
            
            return 0
        })
        
        guard let _ = TemperatureModel.day(date: date) else {
            insert()
            return
        }
        
        update()
        
    }
    

    public static func getTemperature24ForSleep(date: Date) ->TemperatureModel {
        
        let sqlDate = date.sqlDate()
        let yesterDate = date - 1.days
        
        var todyModel: TemperatureModel = TemperatureModel()
        todyModel.date = date
        
        var yesterModel: TemperatureModel = TemperatureModel()
        todyModel.date = yesterDate
        
        if let model = TemperatureModel.day(date: sqlDate) {
            let tuple = SleepModel.sleepTimeDataMapToTuple(datas: model.tempeartures, sleepDate: date, minute: 5)
            model.tempeartures = tuple.morningWakeUp
            todyModel = model
        }
        
        if let model = TemperatureModel.day(date: yesterDate) {
            let tuple = SleepModel.sleepTimeDataMapToTuple(datas: model.tempeartures, sleepDate: date, minute: 5)
            model.tempeartures = tuple.tonightFallASleep
            yesterModel = model
        }
        
        let model = TemperatureModel()
        model.id = todyModel.id
        model.adress = todyModel.adress
        model.date = todyModel.date
        model.tempeartures.append(contentsOf: yesterModel.tempeartures)
        model.tempeartures.append(contentsOf: todyModel.tempeartures)
        
        return model
            
    }
    
    public static func getTemperatureForSleep(date: Date) ->[TemperatureModel] {
        
        let date = date.sqlDate()
        let yesterDate = date - 1.days
        
        var todyModel: TemperatureModel = TemperatureModel()
        todyModel.date = date
        
        var yesterModel: TemperatureModel = TemperatureModel()
        todyModel.date = yesterDate
        
        if let model = [TemperatureModel].deserialize(from: date)?.first  {
            todyModel = model
        }
        
        if let model = [TemperatureModel].deserialize(from: yesterDate)?.first {
            yesterModel = model
        }
        
        return [yesterModel, todyModel]
        
    }
    
    public static func all() ->[TemperatureModel] {
        
        let list = [TemperatureModel].all().flatMap ({ return $0.isEmpty ? [TemperatureModel()] : $0 })
        
        return list
    }
    
}


